package com.shiku.mylibrary.service;


import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.RemoteException;
import android.support.v4.content.LocalBroadcastManager;

import com.shiku.mylibrary.GuardAidl;
import com.shiku.mylibrary.config.NotificationUtils;
import com.shiku.mylibrary.receiver.NotificationClickReceiver;

/**
 * 守护进程
 */
@SuppressWarnings(value = {"unchecked", "deprecation"})
public final class RemoteService extends Service {
    private boolean isConnected = false;
    private final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Intent remoteService = new Intent(RemoteService.this,
                    LocalService.class);
            RemoteService.this.startService(remoteService);
            isConnected = RemoteService.this.bindService(new Intent(RemoteService.this,
                    LocalService.class), connection, Context.BIND_ABOVE_CLIENT);
            PowerManager pm = (PowerManager) RemoteService.this.getSystemService(Context.POWER_SERVICE);
            boolean isScreenOn = pm.isScreenOn();
            // 保活组件的广播action不加包名，互相串线的实际效果有待测试，
            if (isScreenOn) {
                LocalBroadcastManager.getInstance(RemoteService.this).sendBroadcast(new Intent("_ACTION_SCREEN_ON"));
            } else {
                LocalBroadcastManager.getInstance(RemoteService.this).sendBroadcast(new Intent("_ACTION_SCREEN_OFF"));
            }
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        }
    };
    private MyBilder mBilder;

    @Override
    public void onCreate() {
        super.onCreate();
        if (mBilder == null) {
            mBilder = new MyBilder();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBilder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            isConnected = this.bindService(new Intent(RemoteService.this, LocalService.class),
                    connection, Context.BIND_ABOVE_CLIENT);
        } catch (Exception e) {
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isConnected) {
            unbindService(connection);
            isConnected = false;
        }

    }

    private final class MyBilder extends GuardAidl.Stub {
        @Override
        public void wakeUp(String title, String discription, int iconRes) throws RemoteException {
            if (Build.VERSION.SDK_INT < 25) {
                Intent intent2 = new Intent(getApplicationContext(), NotificationClickReceiver.class);
                intent2.setAction(NotificationClickReceiver.CLICK_NOTIFICATION);
                Notification notification = NotificationUtils.createNotification(RemoteService.this, title, discription, iconRes, intent2);
                RemoteService.this.startForeground(13687, notification);
            }
        }

    }

}
